;(function(){
	
	let t = null
	
	let Slider = function(){
		this.oSliderWrap = $(".J_sliderWrap");
		this.sliderItems = this.oSliderWrap.find('.item');
		this.sliderLen = this.sliderItems.length;
		this.indicator = this.oSliderWrap.find('.indicator');
		this.indicatorItems = this.indicator.find(".indicator-item");
		this.sliderBtn = this.oSliderWrap.find('.J_sliderBtn');
		this.index = 0;
	}
	
	$.extend(Slider.prototype, {
		init: function(){
			this.bindEvent();
			this.autoPlay();
		},
		
		bindEvent: function(){
			this.oSliderWrap.on('mouseenter',this.mouseIn);
			this.oSliderWrap.on('mouseleave',$.proxy(this.mouseOut,this));
			this.indicator.on('mouseenter','.indicator-item',$.proxy(this.manualSlide,this));
			this.sliderBtn.on('click',$.proxy(this.btnClick,this));
		},
		
		autoPlay: function(){
			t = setInterval(this.autoSlide.bind(this),1000)
		},
		
		mouseIn: function(){
			clearInterval(t);
		},
		
		mouseOut: function(){
			this.autoPlay()
		},
		
		autoSlide: function(){
			if(this.index >= this.sliderLen - 1){
				this.index = 0;
			}else{
				this.index ++;
			}
			this._slideAction(this.index)
		},
		
		manualSlide: function(e){
			var e = e || window.event,
				tar = e.target || e.srcElement,
				tagName = tar.tagName.toLowerCase();
			if(tagName === 'i'){
				this.index = Array.prototype.indexOf.call(this.indicatorItems,tar)
				this._slideAction(this.index)
			}	
		},
		
		btnClick: function(e){
			var e = e || event,
				tar = e.target || e.srcElement,
				dir = tar.getAttribute('data-dir');
			switch(dir){
				case 'forward':
					if(this.index >= this.sliderLen - 1){
						this.index = 0;
					}else{
						this.index ++;
					}
					break;
				case 'backward':
					if(this.index <= 0){
						this.index = this.sliderLen - 1;
					}else{
						this.index --;
					}
					break;
				default:
					break;
			}
			this._slideAction(this.index)
		},
		
		_slideAction: function(idx){
			this.sliderItems.eq(idx).fadeIn(800)
							.siblings().fadeOut(800);
			this.indicatorItems.eq(idx).addClass('cur')
									  .siblings().removeClass('cur');
		}
	})
	
	new Slider().init()
})();